﻿using Dapper;
using OMG.Fill.Mulberry.Model;
using OMG.Fill.Mulberry.Model.Ent;
using ProjectShare;
using System;
using System.Collections.Generic;
using System.Linq;
namespace OMG.Fill.Mulberry.Service
{
    /// <summary>
    /// 行政区划表T_Area通用操作类
    /// </summary>
    public class AreaServices:Areas
    {
        public Pager<List<AreaEntity>> GetPageList(AreaQuery query)
        {
            Pager<List<AreaEntity>> obj = new Pager<List<AreaEntity>>();
            var paramlist = new DynamicParameters();
            string strWhereSql = "";//from 表在下面，需完善其他查询条件在这里接的写，格式为："and xxx=@xxx空格,如下注释部分代码所示："
            if (!string.IsNullOrEmpty(query.Name))
            {
                strWhereSql += "and Name like @Name ";
                paramlist.Add("Name", "%" + query.Name + "%");
            }
            if (!string.IsNullOrEmpty(query.Code))
            {
                strWhereSql += "and Code = '@Code' ";
                paramlist.Add("Code", query.Code);
            }
            if (!string.IsNullOrEmpty(query.ParentCode))
            {
                strWhereSql += "and ParentCode = '@ParentCode' ";
                paramlist.Add("ParentCode", query.ParentCode);
            }
            string strFromSql = "from dbo.T_Area where 1=1 " + strWhereSql;
            string strTotal = "select count(1)" + strFromSql + strWhereSql;
            string strOrderSql = "";//自定义完成order by语句
            string strSql = @"select * from (select *,ROW_NUMBER() over (order by Id ) as RowNumber " + strFromSql + ") as t ";
            strSql += "where t.RowNumber between  @minRow and @maxRow" + strOrderSql;
            paramlist.Add("minRow", query.MinRow);
            paramlist.Add("maxRow", query.MaxRow);
            try
            {
                using (var dbCon = ConnectFactory.GetOpenConnection())
                {
                    obj.rows = dbCon.Query<AreaEntity>(strSql,paramlist).ToList();
                    obj.total = dbCon.Query<int>(strTotal,paramlist).FirstOrDefault();
                    obj.status = DefaultKey.SuccessResult;
                    obj.message = "获取成功！";
                    dbCon.Dispose();//数据库连接释放
                    return obj;
                }
            }
            catch (Exception ex)
            {
                obj.status = DefaultKey.ErrorResult;
                obj.message = ex.ToString().Trim();
                LogHelper.ErrorLog("获取行政区划表T_Area分页列表失败", ex);
                return obj;
            }
        }


        public ResResult<List<CascaderTreeModel>> GetCascaderTreeList(string id)
        {
            ResResult<List<CascaderTreeModel>> list = new ResResult<List<CascaderTreeModel>>();
            list.model = GetAreaTreeList(id);
            list.status = DefaultKey.SuccessResult;
            list.message = "获取行政区划列表成功！";
            return list;
        }
        public ResResult<List<AreaTreeModel>> GetAreaModelTreeList(string id)
        {
            ResResult<List<AreaTreeModel>> list = new ResResult<List<AreaTreeModel>>();
            list.model = GetAreaModelList(id);
            list.status = DefaultKey.SuccessResult;
            list.message = "获取行政区划列表成功！";
            return list;
        }

        public List<CascaderTreeModel> GetAreaTreeList(string id)
        {
            List<CascaderTreeModel> listArea = new List<CascaderTreeModel>();
            string strSql = @"select * from [dbo].[T_Area] where ParentCode = @ParentCode order by Code asc";
            try
            {
                using (var dbCon = ConnectFactory.GetOpenConnection())
                {
                    List<AreaEntity> tempList = dbCon.Query<AreaEntity>(strSql, new { ParentCode = id }).ToList();
                    foreach (var item in tempList)
                    {
                        CascaderTreeModel TreeModel = new CascaderTreeModel();
                        TreeModel = new CascaderTreeModel();
                        TreeModel.label = item.Name;
                        TreeModel.value = item.Code;
                        TreeModel.FullName = item.FullName;
                        TreeModel.coord = item.coord;
                        TreeModel.children = GetAreaTreeList(item.Code).Count == 0 ? new List<CascaderTreeModel>() : GetAreaTreeList(item.Code);
                        listArea.Add(TreeModel);
                    }
                    return listArea;
                }
            }
            catch (Exception ex)
            {
                LogHelper.ErrorLog("初始化行政区划级联选择数据集失败", ex);
                return listArea;
            }
        }
        public List<AreaTreeModel> GetAreaModelList(string id)
        {
            List<AreaTreeModel> listArea = new List<AreaTreeModel>();
            string strSql = @"select * from [dbo].[T_Area] where ParentCode = @ParentCode order by Code asc";
            try
            {
                using (var dbCon = ConnectFactory.GetOpenConnection())
                {
                    List<AreaEntity> tempList = dbCon.Query<AreaEntity>(strSql, new { ParentCode = id }).ToList();
                    foreach (var item in tempList)
                    {
                        AreaTreeModel TreeModel = new AreaTreeModel();
                        TreeModel = new AreaTreeModel();
                        TreeModel.expand = false;
                        TreeModel.@checked = false;
                        TreeModel.Id= item.Code;
                        TreeModel.title = item.Name;
                        TreeModel.Code = item.Code;
                        TreeModel.children = GetAreaModelList(item.Code).Count == 0 ? new List<AreaTreeModel>() : GetAreaModelList(item.Code);
                        listArea.Add(TreeModel);
                    }
                    return listArea;
                }
            }
            catch (Exception ex)
            {
                LogHelper.ErrorLog("初始化行政区划级联选择数据集失败", ex);
                return listArea;
            }
        }

        public ResResult<List<CascaderModel>> GetCascaderList(string id)
        {
            ResResult<List<CascaderModel>> list = new ResResult<List<CascaderModel>>();
            list.model = GetAreaList(id);
            list.status = DefaultKey.SuccessResult;
            list.message = "获取行政区划列表成功！";
            return list;
        }

  
        public List<CascaderModel> GetAreaList(string id)
        {
            List<CascaderModel> listArea = new List<CascaderModel>();
            string strSql = @"select * from [dbo].[T_Area] where ParentCode=@ParentCode order by Code asc";
            try
            {
                using (var dbCon = ConnectFactory.GetOpenConnection())
                {
                    List<AreaEntity> tempList = dbCon.Query<AreaEntity>(strSql, new { ParentCode = id }).ToList();
                    foreach (var item in tempList)
                    {
                        CascaderModel TreeModel = new CascaderModel();
                        TreeModel = new CascaderModel();
                        TreeModel.label = item.Name;
                        TreeModel.value = item.Code;
                        if (GetAreaCount(item.Code) != 0)
                        {
                            TreeModel.children = new List<CascaderModel>();
                        }
                        else
                        {
                            TreeModel.children = null;
                        }
                        listArea.Add(TreeModel);
                    }
                    return listArea;
                }
            }
            catch (Exception ex)
            {
                LogHelper.ErrorLog("初始化行政区划级联选择数据集失败", ex);
                return listArea;
            }
        }

        public List<AreaListModel> GetAreaCodeList(string ParentCode) {
            List<AreaListModel> listAreaCode = new List<AreaListModel>();
            string strSql = @"select Code,FullName,Coord from [dbo].[T_Area] where ParentCode=@ParentCode order by Code asc";
            try
            {
                using (var dbCon = ConnectFactory.GetOpenConnection())
                {
                    List<AreaEntity> tempList = dbCon.Query<AreaEntity>(strSql, new { ParentCode = ParentCode }).ToList();
                    foreach (var item in tempList)
                    {
                        AreaListModel model = new AreaListModel();
                        model.AreaCode = item.Code;
                        model.Coord = item.coord;
                        model.AreaName = item.FullName;
                        listAreaCode.Add(model);
                    }
                    return listAreaCode;
                }
            }
            catch (Exception ex)
            {
                LogHelper.ErrorLog("获取下属行政区划数据集失败", ex);
                return listAreaCode;
            }
        }


        public int GetAreaCount(string id)
        {
            string strSql = @"select * from [dbo].[T_Area] where ParentCode=@ParentCode";
            try
            {
                using (var dbCon = ConnectFactory.GetOpenConnection())
                {
                    List<AreaEntity> tempList = dbCon.Query<AreaEntity>(strSql, new { ParentCode = id }).ToList();
                    return tempList.Count;
                }
            }
            catch (Exception ex)
            {
                LogHelper.ErrorLog("初始化行政区划级联选择数据集失败", ex);
                return 0;
            }
        }


        public string GetParentCodes(string code)
        {
            string parentCode = GetParentCode(code);
            return parentCode;
        }

        public string GetParentCode(string code)
        {
            string parentCode = "";
            string strSql = @"select ParentCode from dbo.T_Area where Code = @Code";
            try
            {
                using (var dbCon = ConnectFactory.GetOpenConnection())
                {
                    parentCode = dbCon.Query<string>(strSql, new { Code = code }).FirstOrDefault();
                    if (!string.IsNullOrEmpty(parentCode) && parentCode != "530000000000")
                    {
                        parentCode = GetParentCode(parentCode) + "," + parentCode;
                    }
                    return parentCode;
                }
            }
            catch (Exception ex)
            {
                return parentCode;
            }
        }

        public virtual ResResult<AreaEntity> GetByCodeModel(string code)
        {
            ResResult<AreaEntity> obj = new ResResult<AreaEntity>();
            string strSql = @"select * from dbo.T_Area where code = @code";
            try
            {
                using (var dbCon = ConnectFactory.GetOpenConnection())
                {
                    obj.model = dbCon.Query<AreaEntity>(strSql, new { code = code }).FirstOrDefault();
                    obj.status = DefaultKey.SuccessResult;
                    obj.message = "获取成功！";
                    dbCon.Dispose();//数据库连接释放
                    return obj;
                }
            }
            catch (Exception ex)
            {
                obj.status = DefaultKey.ErrorResult;
                obj.message = ex.ToString().Trim();
                LogHelper.ErrorLog("获取行政区划表T_Area对象信息失败", ex);
                return obj;
            }
        }

        /// <summary>
        /// 获取县级Code列表
        /// </summary>
        /// <returns></returns>
        public List<string> GetCountyList()
        {
            List<string> templist = new List<string>();
            string strSql = @"select Code from dbo.T_Area where ParentCode != '530000000000' and ParentCode!=''";
            try
            {
                using (var dbCon = ConnectFactory.GetOpenConnection())
                {
                    templist = dbCon.Query<string>(strSql).ToList();
                    return templist;
                }
            }
            catch (Exception ex)
            {
                return templist;
            }
        }

        /// <summary>
        /// 获取州级Code列表
        /// </summary>
        /// <returns></returns>
        public List<string> GetPrefectureList()
        {
            List<string> templist = new List<string>();
            string strSql = @"select Code from dbo.T_Area where ParentCode = '530000000000' order by Code asc";
            try
            {
                using (var dbCon = ConnectFactory.GetOpenConnection())
                {
                    templist = dbCon.Query<string>(strSql).ToList();
                    return templist;
                }
            }
            catch (Exception ex)
            {
                return templist;
            }
        }

        /// <summary>
        /// 获取全省Code列表
        /// </summary>
        /// <returns></returns>
        public List<string> GetProvinceList()
        {
            List<string> templist = new List<string>();
            string strSql = @"select Code from dbo.T_Area where ParentCode = '530000000000'";
            try
            {
                using (var dbCon = ConnectFactory.GetOpenConnection())
                {
                    templist = dbCon.Query<string>(strSql).ToList();
                    return templist;
                }
            }
            catch (Exception ex)
            {
                return templist;
            }
        }
    }
}